import gtk
import server
import main

class WndAddContact:
    
    def __init__(self, parent):
        self._builder = gtk.Builder()
        self._builder.add_from_file("add_contact.glade")
        self._builder.connect_signals(self)
        
        self.parent = parent
        
        self._obj = self._builder.get_object("wnd_add_contact")
        self._obj.set_transient_for(self.parent._obj)
        self._obj.show_all()
        
    def on_cmd_add_contact_ok_activate(self, widget, data = None):
        name = self._builder.get_object("txt_add_contact").get_text()
        
        resp, err = server.add_to_contact_list(server.username, name)
        # resp may be: Unknown, Present, Self, Success
        
        if resp == "Success":
            contact_list.populate()

            self._obj.destroy()
            return
            
        if   resp == "Unknown": msg = "Contact does not exists."
        elif resp == "Present": msg = "The contact is already in your list."
        elif resp == "Self"   : msg = "You are trying to add yourself!!"
        else: msg == "An unknown error as occurred."

        diag = gtk.MessageDialog(flags=gtk.DIALOG_MODAL,parent=self.parent._obj, type=gtk.MESSAGE_ERROR,buttons=gtk.BUTTONS_OK,message_format=msg)
        diag.run()
        diag.destroy()


    def on_cmd_add_contact_cancel_activate(self, widget, data = None):
        self._obj.destroy()
